<!--
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 * No deletion without permission, or be held responsible to law.
 * @author FIRE
-->
<template>
  <BasicDrawer v-bind="$attrs" :showFooter="false" @register="registerDrawer2" width="80%">
    <!-- :maskClosable="false" -->
    <template #title>
      <Icon :icon="getTitle.icon" class="pr-1 m-1" />
      <span> 模板明细管理 </span>
    </template>
    <div>
      <BasicTable @register="registerTable">
        <template #tableTitle>
          <!-- <Icon :icon="getTitle.icon" class="m-1 pr-1" /> -->
          <span>当前模板： （{{ zbParams.code }}） {{ zbParams.name }} </span>
        </template>
        <template #toolbar>
          <a-button
            type="primary"
            @click="handleCheck()"
            v-auth="'setting:pricemodel:wecharPriceModel:edit'"
          >
            <Icon icon="simple-line-icons:shuffle" /> {{ t('数据合法性检查') }}
          </a-button>
          <a-popconfirm
            title="是否确认清除所有明细？"
            ok-text="是"
            cancel-text="否"
            @confirm="handleClear()"
          >
            <a-button type="primary" v-auth="'setting:pricemodel:wecharPriceModel:edit'">
              <Icon icon="ant-design:delete-outlined" /> {{ t('清除所有明细') }}
            </a-button>
          </a-popconfirm>
          <a-button type="default" @click="handleExport()">
            <Icon icon="ant-design:download-outlined" /> {{ t('导出') }}
          </a-button>
          <a-button type="default" @click="handleImport()">
            <Icon icon="ant-design:upload-outlined" /> {{ t('导入') }}
          </a-button>
          <a-button
            type="primary"
            @click="handleForm({})"
            v-auth="'setting:pricemodel:wecharPriceModel:edit'"
          >
            <Icon icon="fluent:add-12-filled" /> {{ t('新增') }}
          </a-button>
        </template>
        <template #firstColumn="{ record }">
          <a @click="handleForm({ id: record.id })">
            {{ record.invCode }}
          </a>
        </template>
      </BasicTable>
      <InputForm @register="registerDrawer" @success="handleSuccess" />
      <FormImport @register="registerImportModal" @success="handleSuccess" />
    </div>
  </BasicDrawer>
</template>
<script lang="ts">
  export default defineComponent({
    name: 'ViewsSettingPricemodelWecharPriceModelInvList',
  });
</script>
<script lang="ts" setup>
  import { defineComponent, ref } from 'vue';
  import { useI18n } from '/@/hooks/web/useI18n';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { useGlobSetting } from '/@/hooks/setting';
  import { downloadByUrl } from '/@/utils/file/download';
  import { router } from '/@/router';
  import { Icon } from '/@/components/Icon';
  import { BasicTable, BasicColumn, useTable } from '/@/components/Table';
  import {
    wecharPriceModelInvDelete,
    wecharPriceModelInvListData,
    wecharPriceModelInvCheck,
    wecharPriceModelInvClear,
  } from '/@/api/setting/pricemodel/wecharPriceModelInv';
  import { basInvTreeData2 } from '/@/api/bas/inv/basInv';
  import { useDrawer, BasicDrawer, useDrawerInner } from '/@/components/Drawer';
  import { useModal } from '/@/components/Modal';
  import { FormProps } from '/@/components/Form';
  import InputForm from './form.vue';
  import FormImport from './formImport.vue';

  const { t } = useI18n('setting.pricemodel.wecharPriceModelInv');
  const { showMessage } = useMessage();
  const getTitle = {
    icon: router.currentRoute.value.meta.icon || 'ant-design:book-outlined',
    // value: router.currentRoute.value.meta.title || t('模板明细管理'),
  };
  const zbParams = ref<any>({});

  const searchForm: FormProps = {
    baseColProps: { lg: 6, md: 8 },
    labelWidth: 90,
    schemas: [
      // {
      //   label: t('模板编号'),
      //   field: 'modelCode',
      //   component: 'Input',
      // },
      {
        label: t('存货编码'),
        field: 'basInv.cinvcode',
        component: 'TreeSelect',
        componentProps: {
          api: basInvTreeData2,
          allowClear: true,
          onChange: handleSuccess,
        },
      },
      {
        label: t('存货名称'),
        field: 'basInv.cinvname',
        component: 'Input',
      },
      {
        label: t('协同路线'),
        field: 'croute',
        component: 'Input',
      },
      {
        label: t('备注'),
        field: 'remarks',
        component: 'Input',
      },
      {
        label: t('状态'),
        field: 'cstatus',
        component: 'Select',
        componentProps: {
          dictType: 'sys_check_status',
          allowClear: true,
          onChange: handleSuccess,
        },
      },
    ],
  };

  const tableColumns: BasicColumn[] = [
    // {
    //   title: t('模板编号'),
    //   dataIndex: 'modelCode',
    //   key: 'a.model_code',
    //   sorter: true,
    //   width: 230,
    //   align: 'left',
    //   slot: 'firstColumn',
    // },
    {
      title: t('存货编码'),
      dataIndex: 'invCode',
      key: 'a.inv_code',
      sorter: true,
      width: 100,
      align: 'left',
      slot: 'firstColumn',
    },
    {
      title: t('存货名称'),
      dataIndex: 'basInv.cinvname',
      key: 'a.inv_code',
      sorter: true,
      width: 150,
      align: 'left',
    },
    {
      title: t('协同路线'),
      dataIndex: 'croute',
      key: 'a.croute',
      sorter: true,
      width: 100,
      align: 'left',
    },

    {
      title: t('协同单价'),
      dataIndex: 'routePrice',
      key: 'a.route_price',
      sorter: true,
      width: 100,
      align: 'left',
    },
    {
      title: t('协同描述'),
      dataIndex: 'routeDesc',
      key: 'a.route_desc',
      sorter: true,
      width: 150,
      align: 'left',
    },
    {
      title: t('备注'),
      dataIndex: 'remarks',
      key: 'a.remarks',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('检查结果'),
      dataIndex: 'checkResult',
      key: 'a.check_result',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('状态'),
      dataIndex: 'cstatus',
      key: 'a.cstatus',
      sorter: true,
      width: 80,
      align: 'center',
      dictType: 'sys_check_status',
      fixed: 'right',
    },
  ];

  const actionColumn: BasicColumn = {
    width: 100,
    actions: (record: Recordable) => [
      {
        icon: 'clarity:note-edit-line',
        title: t('编辑模板明细'),
        onClick: handleForm.bind(this, { id: record.id }),
        auth: 'setting:pricemodel:wecharPriceModel:edit',
      },
      {
        icon: 'ant-design:delete-outlined',
        color: 'error',
        title: t('删除模板明细'),
        popConfirm: {
          title: t('是否确认删除模板明细'),
          confirm: handleDelete.bind(this, { id: record.id }),
        },
        auth: 'setting:pricemodel:wecharPriceModel:edit',
      },
    ],
  };

  const [registerDrawer, { openDrawer }] = useDrawer();
  const [registerTable, { reload, getForm }] = useTable({
    api: wecharPriceModelInvListData,
    beforeFetch: (params) => {
      params.modelCode = zbParams.value.code;
      return params;
    },
    columns: tableColumns,
    actionColumn: actionColumn,
    formConfig: searchForm,
    showTableSetting: true,
    useSearchForm: true,
    canResize: true,
    immediate: false,
  });

  function handleForm(record: Recordable) {
    openDrawer(true, { ...record, modelCode: zbParams.value.code });
  }

  async function handleExport() {
    const { ctxAdminPath } = useGlobSetting();
    downloadByUrl({
      url:
        ctxAdminPath +
        '/setting/pricemodel/wecharPriceModelInv/exportData?modelCode=' +
        zbParams.value.code,
      target: '_self',
    });
  }

  const [registerImportModal, { openModal: importModal }] = useModal();

  function handleImport() {
    importModal(true, {});
  }

  async function handleDelete(record: Recordable) {
    const res = await wecharPriceModelInvDelete(record);
    showMessage(res.message);
    handleSuccess();
  }

  async function handleCheck() {
    const res = await wecharPriceModelInvCheck({ selIds: zbParams.value.code });
    showMessage(res.message);
    handleSuccess();
  }

  async function handleClear() {
    const res = await wecharPriceModelInvClear({ modelCode: zbParams.value.code });
    showMessage(res.message);
    handleSuccess();
  }

  function handleSuccess() {
    reload();
  }

  const [registerDrawer2, { setDrawerProps, closeDrawer }] = useDrawerInner(async (data) => {
    setDrawerProps({ loading: true });
    zbParams.value = data;
    // await resetFields();
    // const res = await wecharPriceModelForm(data);
    // record.value = (res.wecharPriceModel || {}) as WecharPriceModel;
    // record.value.__t = new Date().getTime();
    // setFieldsValue(record.value);
    // updateSchema([
    //   {
    //     field: 'code',
    //     componentProps: {
    //       disabled: !record.value.isNewRecord,
    //     },
    //   },
    // ]);
    handleSuccess();
    setDrawerProps({ loading: false });
  });
</script>
